package com.turing.post.tuxedo.util;

import java.io.Serializable;
import java.util.Date;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.SHA1;

/**
 * 商户交易应答基类
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
public class TransResponse implements Serializable {
	/**
	 * 交易码
	 */
	public String tranCode = "";

	/**
	 * 受理时间
	 */
	public Date tranTime = new Date();

	/**
	 * 受理方标识码
	 */
	public String acqInstId = "";

	/**
	 * 主机流水号
	 */
	public String ackSeqNo = "";

	/**
	 * 交易MAC
	 */
	public String tranMac = "";

	/**
	 * 交易KEY
	 */
	public String tranKey = "";

	/**
	 * 成功
	 */
	public static final String RESPCODE_SUCC = "00";

	/**
	 * 响应码
	 */
	public String respCode = "";

	/**
	 * 错误信息
	 */
	public String respErrMsg = "";

	/**
	 * 计算MAC的字符串
	 */
	public StringBuilder macBuf = null;

	/**
	 * 产生计算MAC的域
	 */
	protected void generateMacField() {
	}

	/**
	 * 产生MAC
	 * 
	 * @return
	 */
	public void genMac() {
		macBuf = new StringBuilder();
		// 本类
		appendMacField(tranCode);
		// 子类
		generateMacField();
		macBuf.append(tranKey);
		tranMac = new SHA1().getDigestOfString(macBuf.toString().getBytes());
	}

	/**
	 * 增加计算MAC的域
	 * 
	 * @param buff
	 */
	public void appendMacField(String buff) {
		macBuf.append(PCPSUtil.isEmpty(buff) ? "" : buff.trim());
	}

	/**
	 * 构造交易应答私有报文体
	 * 
	 * @param reqBody
	 * @throws Exception
	 */
	protected void constructRespBodyXml(Element respBody)
			throws PCPSApplicationException {
	}

	/**
	 * 构造XML应答报文
	 * 
	 * @param doc
	 */
	public byte[] constructXml() throws PCPSApplicationException {
		// genMac
		genMac();

		Element root = new Element(TransXmlHelper.XML_NODE_TransResponse);
		// tranCode
		TransXmlHelper.addStringElement(root, TransXmlHelper.XML_NODE_TranCode,
				tranCode);
		// tranTime
		TransXmlHelper.addStringElement(root, TransXmlHelper.XML_NODE_TranTime,
				PCPSUtil.getStringYMDHMSPackedByDate(tranTime));
		// respCode
		TransXmlHelper.addStringElement(root, TransXmlHelper.XML_NODE_RespCode,
				respCode);
		if (RESPCODE_SUCC.equals(respCode)) {
			// ackSeqNo
			TransXmlHelper.addStringElement(root,
					TransXmlHelper.XML_NODE_AckSeqNo, ackSeqNo);
			Element respbody = new Element(TransXmlHelper.XML_NODE_RespBody);
			constructRespBodyXml(respbody);
			root.addContent(respbody);
		} else {
			// respErrMsg
			TransXmlHelper.addStringElement(root,
					TransXmlHelper.XML_NODE_RespErrMsg, respErrMsg);
		}
		// tranMac
		TransXmlHelper.addStringElement(root, TransXmlHelper.XML_NODE_TranMac,
				tranMac);
		Document doc = new Document(root);
		XMLOutputter out = new XMLOutputter();
		out.setFormat(Format.getPrettyFormat().setEncoding(
				TransXmlHelper.XML_CHARSET));
		try {
			return out.outputString(doc).trim().getBytes(
					TransXmlHelper.XML_CHARSET);
		} catch (Exception e) {
			ParaseException.TransParamException("打包输出XML错误!");
		}
		return null;
	}
}
